<?php
/*******************************************************************************
 * LightPress - http://lightpress.org
 *
 * Copyright 2004-2005 Ludovico Magnocavallo.
 * Released under the GPL http://www.gnu.org/copyleft/gpl.html
 *
 * $Id: feed.php 557 2006-10-09 08:41:30Z ludo $
 ******************************************************************************/

require_once 'config.php';

// handle different feed formats
$feed_type = isset($_REQUEST['feed']) ? $_REQUEST['feed'] : "rss2";
	
switch($feed_type) {
    // TODO: add RSS 0.91/2???  Atom1.0?
    case 'atom':
        $feed_file = 'atom.xml';
        $feed_content = 'text/xml';
        $feed_date = "Y-m-d\TH:i:s";	// unused
        $tag_element = 'dc:subject';
        break;
    default:
        //  assume RSS2.0
        $feed_file = "rss2.xml";
        $feed_content = "text/xml"; // or 'application/rss+xml'?
        $feed_date = "D, d M Y H:i:s";	// RFC 2822 format Mon, 11 Jul 2005 17:04:21
        $tag_element = 'subject';
        break;
}

$f =& new Frontend($db_options, $options, null, $feed_type); // cache does not support feeds yet

$db =& $f->db;
$tpl =& $f->tpl;
$messages =& $f->messages;

$num_posts = $options['posts_per_rss'];

// Get GMT timestamp of last post modification

$tstamp = $f->getLastModified(false);

// ugh, send a content-type even if it should not be needed for 304s
// http://support.microsoft.com/default.aspx?scid=kb;en-us;831140&Product=ISAS

// we seem to be doing it right: http://www.xml.com/pub/a/2004/07/21/dive.html
header("Content-type: $feed_content; charset=" . $options['charset'], true);
header('X-Pingback: ' . $options['url'] . '/xmlrpc.php');

$last_modified_gmt = $f->doConditionalGet($tstamp);

if (empty($f->options['description']))
    $tpl->setVar('option_description', 'A WordPress+Lightpress-powered blog');

if ($feed_type == 'atom') {
    // http://feedvalidator.org/docs/error/InvalidW3CDTFDate.html
    $last_modified_gmt = $f->ISO8601($tstamp);
    
    // http://feedvalidator.org/docs/error/InvalidLanguage.html
    $tpl->setVar('option_lang', str_replace('_', '-', $f->options['lang']));
    $tpl->setVar('option_copyright_notice', htmlentities($f->options['copyright_notice']));
}

$posts =& $f->getPosts(null, null, 0, $num_posts, false, true);

$tpl->setFile('index', $feed_file);
$tpl->setVar('pub_date', $last_modified_gmt);

foreach ($posts as $k => $post) {
    if (!empty($post['post_more'])) {
        if (!empty($f->options['rss_use_excerpt']))
            $posts[$k]['post_content'] .= ' <a href="' . $options['url'] . '/' . $post['post_permalink'] . '">' . $messages['post_continued'] . '</a>';
        else
            $posts[$k]['post_content'] .= $post['post_more'];
    }
    if ($feed_type == 'atom') {
        $posts[$k]['post_isodate'] = $f->ISO8601((int)$post['post_tstamp_gmt']);
    } else {
        $posts[$k]['post_isodate'] = date($feed_date, (int)$post['post_tstamp_gmt']) . ' GMT';
    }
    if (isset($post['tags']) && is_array($post['tags']) && count($post['tags']) > 0) {
        foreach ($post['tags'] as $i=>$tag)
            $post['tags'][$i] = htmlspecialchars($tag, ENT_NOQUOTES, $f->options['charset']);
        $post['tags'] = "<$tag_element>" . implode("</$tag_element><$tag_element>", $post['tags']) . "</$tag_element>";
    } else {
        $post['tags'] = '';
    }
    // orrible hack, remember to remove it if/when we implement priority for plugins
    if (isset($post['_tags_set']))
        unset($posts[$k]['_tags_set']);
    unset($posts[$k]['tags']);
    $posts[$k]['tags'] =& $post['tags'];
}
$tpl->parseBlock('ITEM', 'item', $posts, 'index');

$tpl->pparse('MAIN', 'index');

?>
